package stack

import "strings"

//
func isValid(s string) bool {
	stack := make([]rune, 0)
	for _, v := range s {
		if string(v) == "{" || string(v) == "[" || string(v) == "(" {
			stack = append(stack, v)
		} else {
			if len(stack) > 0 && ((string(v) == "}" && string(stack[len(stack)-1]) == "{") || (string(v) == "]" && string(stack[len(stack)-1]) == "[") || (string(v) == ")" && string(stack[len(stack)-1]) == "(")) {
				stack = stack[:len(stack)-1]
			} else {
				return false
			}
		}
	}

	if len(stack) == 0 {
		return true
	} else {
		return false
	}
}

//1021

func RemoveOuterParentheses(s string) string {
	stack := make([]rune, 0)
	ans := make([]rune, 0)

	for _, v := range s {
		if string(v) == "(" {
			if len(stack) > 0 {
				ans = append(ans, v)
			}
			stack = append(stack, v)
		} else if string(v) == ")" {
			if len(stack) > 1 {
				ans = append(ans, v)
			}
			stack = stack[:len(stack)-1]
		}
	}
	return string(ans)
}

// 71
func simplifyPath(path string) string {
	pathList := strings.Split(path, "/")
	ans := []string{}
	for i := 0; i < len(pathList); i++ {
		if pathList[i] == ".." && len(ans) != 0 {
			ans = ans[:len(ans)-1]
		} else if pathList[i] != "." && pathList[i] != "" && pathList[i] != ".." {
			ans = append(ans, pathList[i])
		}
	}
	return "/" + strings.Join(ans, "/")
}
